//
//  ViewController.m
//  BenDiTongZhi
//
//  Created by LTJ on 14-10-31.
//  Copyright (c) 2014年 LTJ. All rights reserved.
//

#import "ViewController.h"

#import <LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(20, 30, 100, 100);
    self.button.backgroundColor = [UIColor blackColor];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    
    
    
}
-(void)buttonAction:(UIButton *)sender
{

    // 结果提示框
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }];
    [alertController addAction:cancelAction];
    
    
    
    // 上下文对象
    LAContext *context = [LAContext new];
    // 存储错误信息
    NSError *error = nil;
    // 需要在弹出框中显示的文字，不可以为空
    NSString *errorReason = @"放上你的手指头 啦啦啦";
    
    
    // 检查设备是不是可以使用biometrics(指纹识别)的方式验证身份（就是看看有没有指纹识别的硬件，或者有硬件但是没设定好指纹识别都是不可以进行验证的）
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { // 可用
        
        // 判断输入的指纹是否正确
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                localizedReason:errorReason
                          reply:^(BOOL success, NSError *error) {
                              
                              // success == YES，则是登录成功，否则错误
                              if (success) {
                                  alertController.title = @"Successed";
                              } else {
                                  alertController.title = @"Failed";
                              }
                              // 弹出提示框
                              [self presentViewController:alertController animated:YES completion:nil];
                          }];
        
    } else { // 不可用
        alertController.title = @"Can not do authentication";
        // 弹出提示框
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
